package fy.uap.dao;

import com.seeyon.ctp.util.UUIDLong;
import com.seeyon.v3x.dee.datasource.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.UUID;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.xpath.DefaultXPath;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.sql.ResultSet;
import com.seeyon.v3x.dee.resource.DbDataSource;

/*import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;*/

public class test0001 {

	
	//主表
	private String CORP_CODE;
	private String ERP_BILL_NO;
	private String CUR;
	private String DEBIT_MONEY;
	private String REIMBURSE_TYPE;
	private String PAYEE_NAME;
	private String PAY_TYPE;
	private String ACCOUNTS;
	private String AMT;
	private String DATA_SOURCE;
	private String ITEM_NUMBER;
	private String REQ_DATE;
	private String ISFORINDIVIDUAL;
	private String PAYEE_BANK;
	private String WISH_PAYDAY;
	private String BUDGET_LINE;
	private String USER_CODE_CREATE;
	private String PURPOSE;
	private String PAYEE_BANK_CODE;
	private String POSTSCRIPT;
	private String PAYEE_ACC_NO;
    //重复表
	private String INTERNAL_TRANSACTIONS;
	private String ERP_BILL_NO2;
	private String CUR2;
	private String AMOUNT;
	private String DEVELOPMENT;
	private String SUBJECT;
	private String REMARKS;
	private String PAY_INFO_ID;
	private String COMPANY;
	private String SUBJECT_1;
	private String CLASSIFY;
	
	private static	String CLASSIFY1="{\"PAYMENT\":[{\"CORP_CODE\":\"1010\",\"ERP_BILL_NO\":\"OA12345681\",\"CUR\":\"CNY\",\"DEBIT_MONEY\":\"800.21\",\"REIMBURSE_TYPE\":\"\",\"PAYEE_NAME\":\"刘海均\",\"PAY_TYPE\":\"34\",\"ACCOUNTS\":\"01\",\"AMT\":\"800.00\",\"DATA_SOURCE\":\"OA\",\"ITEM_NUMBER\":\"\",\"REQ_DATE\":\"2018-11-20\",\"PAYDETAIL\":[{\"INTERNAL_TRANSACTIONS\":\"内部往来\",\"ERP_BILL_NO\":\"OA12345678\",\"CUR\":\"币别\",\"AMOUNT\":\"400.00\",\"DEVELOPMENT\":\"费用部门\",\"SUBJECT\":\"科目\",\"REMARKS\":\"备注\",\"PAY_INFO_ID\":\"p112367\",\"COMPANY\":\"所属公司\",\"SUBJECT_1\":\"子科目\",\"CLASSIFY\":\"分类\"}],\"ISFORINDIVIDUAL\":\"1\",\"PAYEE_BANK\":\"中国银行深圳支行\",\"WISH_PAYDAY\":\"2018-11-22\",\"BUDGET_LINE\":\"400\",\"USER_CODE_CREATE\":\"会计\",\"PURPOSE\":\"福耀集团摘要\",\"PAYEE_BANK_CODE\":\"011177408046\",\"POSTSCRIPT\":\"福耀集团用途\",\"PAYEE_ACC_NO\":\"62235897205899\"}]}\r\n" ;

	

	
	public static void main(String[] args) {
	/*	   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		   
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		String str = "2007-01-18 00:00:00";
		
		try {
			date=format1.parse(str);
			
			System.out.println(formatter.format(date));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/

	/*		   "PAYDETAIL":[
			        		                {
			        		                    "INTERNAL_TRANSACTIONS":"内部往来",
			        		                    "ERP_BILL_NO":"OA12345678",
			        		                    "CUR":"币别",
			        		                    "AMOUNT":"400.00",
			        		                    "DEVELOPMENT":"费用部门",
			        		                    "SUBJECT":"科目",
			        		                    "REMARKS":"备注",
			        		                    "PAY_INFO_ID":"p112367",
			        		                    "COMPANY":"所属公司",
			        		                    "SUBJECT_1":"子科目",
			        		                    "CLASSIFY":"分类"
			        		                }
			        		            ],*/
		 // 获取 数据源
	/*DbDataSource ds1 = getDsByName("OA");
		if (ds1 != null) {
			Connection conn = null;
			Statement st = null;
			ResultSet rs = null;
			try {
				conn = ((JDBCDataSource) ds1).getConnection();// OA创建链接
				st = conn.createStatement(); // 获取jdbc创建连接
				String sql = "select field0002,field0003,field0012,field0023,field0013,field0018,field0016,field0017,field0019,field0014,field0011,field0008,field0015,field0005,field0027,field0022,field0025  from formmain_0017"; // A服务器合同表
				rs = st.executeQuery(sql); // 得到查询出A服务器合同表的结果集
				while (rs.next()) { // 判断结果集中是否存在下一条记录，如果存在则继续循环
					System.out.println(rs.getString(1));
				}
			} catch (SQLException e) {
				System.out.println("异常:" + e.getMessage());
			} finally {
				try {
					if (rs != null) {
						rs.close();
					}
					if (st != null) {
						st.close();
					}
					if (conn != null) {
						conn.close();
					}
				} catch (SQLException e) {
					System.out.println("关闭异常:" + e.getMessage());
				}
			}
		}*/
		
		

		// JSONObject jo =new JSONObject(new TreeMap<String, Object>());
		// 拼接JSON
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		JSONArray ja2 = new JSONArray();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("PAY_INFO_ID","p112367");
		map.put("ERP_BILL_NO","OA12345678");
		map.put("COMPANY","所属公司");
		map.put("AMOUNT","400.00");
		map.put("DEVELOPMENT","费用部门");
		map.put("CUR","币别");
		map.put("SUBJECT","科目");
		map.put("SUBJECT_1","子科目");
		map.put("CLASSIFY","分类");
		map.put("INTERNAL_TRANSACTIONS","内部往来");
		map.put("REMARKS","备注");
		ja2.put(map);
		//ja2.put(map);
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("ERP_BILL_NO", "OA12345681");
		map1.put("REQ_DATE", "2018-11-20");
		map1.put("WISH_PAYDAY", "2018-11-22");
		map1.put("CORP_CODE", "1002");
		map1.put("PAYEE_NAME", "刘海均");
		map1.put("PAYEE_ACC_NO", "62235897205899");
		map1.put("PAYEE_BANK", "中国银行深圳支行");
		map1.put("PAYEE_BANK_CODE", "011177408046");
		map1.put("PAY_TYPE", "34");
		map1.put("REIMBURSE_TYPE","");
		map1.put("AMT","800.00");
		map1.put("CUR","CNY");
		map1.put("ISFORINDIVIDUAL","1");
		map1.put("PURPOSE","福耀集团摘要");
		map1.put("ACCOUNTS","01");
		map1.put( "ITEM_NUMBER","");
		map1.put("BUDGET_LINE","400");
		map1.put("DATA_SOURCE","OA");
		map1.put("POSTSCRIPT","福耀集团用途");
		map1.put("USER_CODE_CREATE","会计");
		map1.put("DEBIT_MONEY","800.21");
		map1.put("PAYDETAIL", ja2);
		ja.put(map1);
		try {
			System.out.println(jo.put("PAYMENT", ja).toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}

		try {
		bandFlowAndFile(jo.put("PAYMENT", ja).toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * Random r = new Random(); // 中间库 数据源 DbDataSource ds = getDsByName("ERP"); //
	 * 获取 数据源 // 中间库Test 数据源 DbDataSource ds1 = getDsByName("OA"); // 获取 数据源 String
	 * flex_value_set_id = ""; String flex_value_meaning = ""; String description =
	 * ""; // executeQuery:单个结果集 executeUpdate:无记录返回(查询,修改) execute:多个结果集 if (ds !=
	 * null && ds1 != null) {
	 * 
	 * Connection conn = null; Statement st = null; ResultSet rs = null;
	 * 
	 * Connection conn1 = null; PreparedStatement pst = null; ResultSet rs1 = null;
	 * try { conn = ((JDBCDataSource) ds).getConnection(); // 创建链接 conn1 =
	 * ((JDBCDataSource) ds1).getConnection();// OA创建链接 st = conn.createStatement();
	 * // 获取jdbc创建连接
	 * 
	 * 
	 * 
	 * //删除 String sqlDelete =
	 * "delete from FORMMAIN_0022 f WHERE f.FIELD0001 IN ('-6690145637470680188','-5340040719096395513','8642655415169730884') "
	 * ; pst = conn1.prepareStatement(sqlDelete); pst.executeUpdate();
	 * 
	 * String sql =
	 * "select  flex_value_set_id,flex_value_meaning,description  from FND_FLEX_VALUES_VL where  flex_value_set_id in ('1009703','1009704','1009705')"
	 * ; // A服务器合同表 rs = st.executeQuery(sql); // 得到查询出A服务器合同表的结果集 while (rs.next())
	 * { // 判断结果集中是否存在下一条记录，如果存在则继续循环 flex_value_set_id = rs.getString(1);
	 * flex_value_meaning = rs.getString(2); description = rs.getString(3);
	 * System.out.println("flex_value_set_id:" + flex_value_set_id +
	 * "flex_value_meaning:" + flex_value_meaning + "description:" + description);
	 * // 转换 if (flex_value_set_id.equals("1009703")) {//分类段 flex_value_set_id =
	 * "8642655415169730884"; } else if (flex_value_set_id.equals("1009704"))
	 * {//内部往来段 flex_value_set_id = "-6690145637470680188"; } else {//备注段
	 * flex_value_set_id = "-5340040719096395513"; } String randomid
	 * =String.valueOf( r.nextLong()); if (flex_value_set_id != "") { String
	 * sqlInsert = "INSERT INTO OATEST.FORMMAIN_0022"+
	 * "(ID, \"STATE\",START_MEMBER_ID, START_DATE, APPROVE_MEMBER_ID, APPROVE_DATE, FINISHEDFLAG, RATIFYFLAG, RATIFY_MEMBER_ID, RATIFY_DATE, SORT, MODIFY_MEMBER_ID, MODIFY_DATE, FIELD0001, FIELD0002, FIELD0003)"
	 * +
	 * "VALUES(?, 1,'4804991247453733398', sysdate, '0', sysdate, 0, 0, '0', NULL, 0, '4804991247453733398', sysdate, ?, ?, ?)"
	 * ; pst = conn1.prepareStatement(sqlInsert); pst.setString(1, randomid);
	 * pst.setString(2, flex_value_set_id); pst.setString(3, flex_value_meaning);
	 * pst.setString(4, description); pst.executeUpdate(); }
	 * 
	 * } } catch (SQLException e) { System.out.println("异常:" + e.getMessage()); }
	 * finally { try { if (rs != null) { rs.close(); } if (st != null) { st.close();
	 * } if (conn != null) { conn.close(); } if (rs1 != null) { rs1.close(); } if
	 * (pst != null) { pst.close(); } if (conn1 != null) { conn1.close(); } } catch
	 * (SQLException e) { System.out.println("关闭异常:" + e.getMessage()); } } }
	 */

	public static void bandFlowAndFile(String ttt) throws Exception   {
		// 创建url地址
		
		
	
		 URL url = new URL("http://10.116.2.53:9000/erp/services/fyjtPaymentWebService.fyjtPaymentWebServiceHttpSoap11Endpoint/");
		 System.out.println(url);
		//		 URL url = new URL(" http://10.116.2.53:9000/erp/services/fyjtPaymentWebService.fyjtPaymentWebServiceHttpEndpoint/");
		// 打开连接
//		URLConnection conn = url.openConnection();
		// 转换成HttpURL
	//	HttpURLConnection httpConn = (HttpURLConnection) conn;
		
		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
		// 打开输入输出的开关
		httpConn.setDoInput(true);
		httpConn.setDoOutput(true);
		// 设置请求方式
		httpConn.setRequestMethod("POST");
		// 设置请求的头信息
		httpConn.setRequestProperty("Content-type", "text/xml;charset=UTF-8");
		// 拼接请求消息
	    String data = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://server.webservice.fyjt.erp.hibernate.byttersoft.com\">" + "<soapenv:Header></soapenv:Header>" + "<soapenv:Body>" + " <ser:sysOaPaymentData>" + "<ser:jsonPayment>" + ttt + "</ser:jsonPayment>" + " </ser:sysOaPaymentData>" + "</soapenv:Body>" + "</soapenv:Envelope>";
		System.out.println("1111"+data);
		// 获得输出流
		OutputStream out = httpConn.getOutputStream();
		// 发送数据
		out.write(data.getBytes());
		System.out.println(httpConn.getResponseCode());
		// 判断请求成功
		if (httpConn.getResponseCode() == 200) {
			// 获得输入流
			InputStream in = httpConn.getInputStream();
			// 使用输入流的缓冲区
			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			StringBuffer sb = new StringBuffer();
			String line = null;
			// 读取输入流
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
			 System.out.println(sb);

			/*
			 * * //创建sax的读取器 SAXReader saxReader = new SAXReader(); //创建文档对象 Document doc
			 * =saxReader.read(new StringReader(sb.toString())); //获得请求响应return元素
			 * List<Element> eles =doc.selectNodes("//return"); for(Element ele : eles){
			 * System.out.println(ele.getText()); }
			 */

			SAXReader saxReader = new SAXReader();
			Document document = saxReader.read(new StringReader(sb.toString()));
			DefaultXPath xpath = new DefaultXPath("//ns:return");
			xpath.setNamespaceURIs(Collections.singletonMap("ns", "http://impl.webservice.srm.fy"));
			List list = xpath.selectNodes(document);
			Iterator iterator = list.iterator();
			while (iterator.hasNext()) {
				Element node = (Element) iterator.next();
				System.out.println("return: " + node.getText());
			}
		}
	}
}